<?php
    
class ControleImovel{

    public function cadastrar(Imovel $imovel) {

		include_once("../conexao/config.php");
		$config = new Config();
		$conexao = $config->conectar();

		$query = "insert into imovel values(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW(), 1, ?)";
		$stmt = $conexao->prepare($query);

		$stmt->bindValue(1, $imovel->getCodigo());
		$stmt->bindValue(2, $imovel->getIdTipo());
		$stmt->bindValue(3, $imovel->getTipoNegocio());
		$stmt->bindValue(4, $imovel->getValor());
		$stmt->bindValue(5, $imovel->getDormitorios());
		$stmt->bindValue(6, $imovel->getSuites());
		$stmt->bindValue(7, $imovel->getGaragem());
		$stmt->bindValue(8, $imovel->getAreaLazer());
		$stmt->bindValue(9, $imovel->getPiscina());
		$stmt->bindValue(10, $imovel->getCaracteristicas());
		$stmt->bindValue(11, $imovel->getIdEndereco());
		$stmt->bindValue(12, $imovel->getFoto());
		$stmt->bindValue(13, $imovel->isLancamento());

		$stmt->execute();   
		
		return $conexao->lastInsertId();       						
	}
	
    public function alterarDadosBasicos(Imovel $imovel) {

		include_once("../conexao/config.php");
		$config = new Config();
		$conexao = $config->conectar();

		$query = "update imovel set codigo_imovel = ?, id_tipo_imovel = ?, tipo_negocio_imovel = ?, valor_imovel = ?, dormitorios_imovel = ?, suites_imovel = ?, garagem_imovel = ?, area_lazer_imovel = ?, piscina_imovel = ?, caracteristicas_imovel = ?, ultima_atualizacao_imovel = NOW(), lancamento=? WHERE id_imovel = ?";
		
		$stmt = $conexao->prepare($query);

		$stmt->bindValue(1, $imovel->getCodigo());
		$stmt->bindValue(2, $imovel->getIdTipo());
		$stmt->bindValue(3, $imovel->getTipoNegocio());
		$stmt->bindValue(4, $imovel->getValor());
		$stmt->bindValue(5, $imovel->getDormitorios());
		$stmt->bindValue(6, $imovel->getSuites());
		$stmt->bindValue(7, $imovel->getGaragem());
		$stmt->bindValue(8, $imovel->getAreaLazer());
		$stmt->bindValue(9, $imovel->getPiscina());
		$stmt->bindValue(10, $imovel->getCaracteristicas());
		$stmt->bindValue(11, $imovel->isLancamento());
		$stmt->bindValue(12, $imovel->getId());
		
		$stmt->execute();
	}
	
	public function alterarEndereco(Imovel $imovel) {

		include_once("../conexao/config.php");
		$config = new Config();
		$conexao = $config->conectar();

		$query = "update imovel set id_endereco_imovel = ?, ultima_atualizacao_imovel = NOW() WHERE id_imovel = ?";
		
		$stmt = $conexao->prepare($query);

		$stmt->bindValue(1, $imovel->getIdEndereco());		
		$stmt->bindValue(2, $imovel->getId());
		
		$stmt->execute();
	}
	
	public function alterarFotoPrincipal(Imovel $imovel, $foto_antiga) {

		//remover foto antiga
		unlink($foto_antiga);

		include_once("../conexao/config.php");
		$config = new Config();
		$conexao = $config->conectar();

		$query = "update imovel set thumb_imovel = ?, ultima_atualizacao_imovel = NOW() WHERE id_imovel = ?";
		
		$stmt = $conexao->prepare($query);

		$stmt->bindValue(1, $imovel->getFoto());		
		$stmt->bindValue(2, $imovel->getId());
		
		$stmt->execute();
	}

		
    public function cadastrarFotoDoImovel($idImovel, $caminho) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "insert into foto_imovel values(NULL, ?, ?)";
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $idImovel);
		$stmt->bindValue(2, $caminho);
		
		$stmt->execute();			
	}
	
	public function pesquisar($id) {
		include_once("../conexao/config.php");
		$conexao = new Config();

		$query = "select * from imovel where id_imovel=?";
		
		$stmt = $conexao->conectar()->prepare($query);
		$stmt->bindValue(1, $id);

		$stmt->execute();

		return $stmt;
	}

    public function pesquisarComFiltro($texto, $filtro) {
		include_once("../conexao/config.php");
		$conexao = new Config();

		$query = "SELECT I.id_imovel, I.codigo_imovel, I.tipo_negocio_imovel, I.valor_imovel, T.nome_tipo, E.bairro FROM imovel I INNER JOIN tipo_imovel T ON I.id_tipo_imovel = T.id_tipo INNER JOIN endereco E ON I.id_endereco_imovel = E.id_endereco WHERE ".$filtro." = '".$texto."' ORDER BY ".$filtro;
		
		$stmt = $conexao->conectar()->prepare($query);

		$stmt->execute();

		return $stmt;
	}
	
	public function pesquisarUltimos() {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "SELECT I.id_imovel, I.codigo_imovel, I.tipo_negocio_imovel, I.valor_imovel, T.nome_tipo, E.bairro FROM imovel I INNER JOIN tipo_imovel T ON I.id_tipo_imovel = T.id_tipo INNER JOIN endereco E ON I.id_endereco_imovel = E.id_endereco order by I.id_imovel DESC limit 0, 10";
		
		$stmt = $conexao->conectar()->prepare($query);				
		
		$stmt->execute();
		
		return $stmt;
	}

	
	public function listarThumb($idImovel) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from foto_imovel where id_imovel = $idImovel";	
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $idImovel);
		
		$stmt->execute();
		
		return $stmt;
	}
	
	public function removerThumb($idThumb, $caminho) {
		
		unlink($caminho);
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "delete from foto_imovel where id_foto= ?";
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $idThumb);
		
		$stmt->execute();
		
	}
	
	public function remover($id) {

		//-----------------excluir thumbs---------------------

		$this->removerThumbs($id);

		//-----------------excluir imóvel---------------------

		include_once("../conexao/config.php");
		$conexao = new Config();
		//$query = "update imovel set ativado_imovel = 0  where id_imovel = ?";
		$query = "delete from imovel where id_imovel=?";
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $id);
		
		$stmt->execute();
				
	}

	public function removerThumbs($id){

	  include_once("../conexao/config.php");
	  $conexao = new Config();
	  //----------------Thumbs secundárias-----------------------------------------------
	  $query = "select imovel_img from foto_imovel where id_imovel = ? ";
	  $stmt = $conexao->conectar()->prepare($query);
	  $stmt->bindValue(1, $id);
	  $stmt->execute();
  
	  foreach ($stmt as $value) {
		  unlink($value['imovel_img']);
	  }
  
	  $query = "delete from foto_imovel where id_imovel = ? ";
	  $stmt = $conexao->conectar()->prepare($query);
	  $stmt->bindValue(1, $id);
	  $stmt->execute();
  
  
	  //----------------Foto principal----------------------------------------------------
	  $query = "select thumb_imovel from imovel where id_imovel = ? ";
	  $stmt = $conexao->conectar()->prepare($query);
	  $stmt->bindValue(1, $id);
	  $stmt->execute();
  
	  foreach ($stmt as $value) {
		 unlink($value['thumb_imovel']);
	  }
  
	}
	
	public function limiteFotos($idImovel) {
		
		include_once("../conexao/config.php");
		$conexao = new Config();
		
		$query = "select * from foto_imovel where id_imovel = $idImovel";	
		
		$stmt = $conexao->conectar()->prepare($query);
		
		$stmt->bindValue(1, $idImovel);
		
		$stmt->execute();
		
		return $stmt->rowCount(PDO::FETCH_ASSOC);
	}

}
?>